--#include "data\config\boss\MayaPalaceConfig.lua" once
--#include "data\config\boss\CommonBossConfig.lua" once


--[[
注册一些事件
]]
function ResisterMayaPalaceProc()
	--print("ResisterMayaPalaceProc...")
	for palaceIdx, palaceCfg in ipairs( MayaPalaceConfig ) do
		SceneEnterDispatcher.register( palaceCfg.sceneId, OnEnterMayaPalace )
		SceneExitDispatcher.register( palaceCfg.sceneId,  OnExitMayaPalace )				--注册离开场景、副本后触发的事件
	end
end

--[[
进入玛雅神殿
]]
function EnterMayaPalace( sysarg, palaceIdx )
	--print("EnterMayaPalace, palaceIdx="..palaceIdx)
	if palaceIdx > 10 then		--这些层不能直接进入
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MayaPalace005,ttFlyTip)
		return
	end

	local palaceCfg = GetMayaPalaceCfg( palaceIdx )
	if not palaceCfg then
		return
	end

	local palaceCfgIn = GetMayaPalaceIdlePlace( palaceCfg )
	--print("EnterMayaPalace, palaceCfgIn.palaceIdx="..palaceCfgIn.palaceIdx)
	CommonEnterScence( sysarg,  palaceCfgIn, GameLog.clMayaPalaceEnterFee, Lang.ScriptTips.MayaPalaceLog001 )
end


function GetMayaPalaceIdlePlace( palaceCfg )
	if not palaceCfg.extPalaces then		--没有扩展层
		return palaceCfg
	end

	local playerList = LuaHelp.getSceneActorListById( palaceCfg.sceneId ) or {}
	--print("GetMayaPalaceIdlePlace, #playerList="..#playerList)
	if #playerList < palaceCfg.playerNum then
		return palaceCfg
	end

	for i, extPalaceIdx in ipairs( palaceCfg.extPalaces ) do
		local extPalaceCfg = GetMayaPalaceCfg( extPalaceIdx )
		local playerList = LuaHelp.getSceneActorListById( extPalaceCfg.sceneId ) or {}
		--print("GetMayaPalaceIdlePlace, extplace, sceneId="..extPalaceCfg.sceneId..", #playerList="..#playerList)
		if not extPalaceCfg.playerNum or #playerList < extPalaceCfg.playerNum then
			return extPalaceCfg
		end
	end

	local extPalaceIdx = palaceCfg.extPalaces[#palaceCfg.extPalaces]
	local extPalaceCfg = GetMayaPalaceCfg( extPalaceIdx )
	return extPalaceCfg
end

--[[
进入玛雅神殿后触发
玛雅神殿只能从外部进入，不能从一层进入另外一层
]]
function OnEnterMayaPalace( sysarg, sceneId, lastSceneId )
	--print("OnEnterMayaPalace, sceneId="..sceneId..", lastSceneId="..lastSceneId)
	local palaceCfg = GetMayaPalaceCfgBySceneId( sceneId )
	if not palaceCfg then
		return
	end
	Actor.closeDialogs( sysarg, enLinkMayaPalaceWnd )
	local dDvar = Actor.getDyanmicVar(sysarg)
	dDvar.enterMayaPalaceTime	= System.getCurrMiniTime()		--进入场景的时间

	OpenMayaPalaceCountDownPanel( sysarg, palaceCfg )
	--注册回城的延迟消息
	Actor.removeEntityScriptDelayMsg( sysarg, 17 )
	--[[最后的true表示实体死亡，消息保留]]
	Actor.postEntityScriptDelayMsg( sysarg, palaceCfg.beInTime*1000, 0, 17,0,0,0,0,false,true )

	System.broadcastTipmsgLimitLev( string.format(Lang.ScriptTips.MayaPalace004, Actor.getName( sysarg )), ttScreenCenter + ttChatWindow,
		palaceCfg.enterLevelLimit[2])

	local restTime = palaceCfg.beInTime
	InitOnlyButtonPanel( sysarg, 2, enRightPanelActivity_MayaPalace, restTime, Lang.ScriptTips.MayaPalaceName )

	Actor.closeDialogs( sysarg, enLinkBossWnd )	
end

--[[
退出个人BOSS副本
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnExitMayaPalace( sysarg, sceneId, fubenId )
	--print("OnExitMayaPalace, sceneId="..sceneId..", fubenId="..fubenId)
	CloseActivityRightPanel(sysarg)			--关闭面板
	CloseCountDownPanel(sysarg)				--关闭面板
	Actor.removeEntityScriptDelayMsg( sysarg, 17 )		--离开BOSS之家场景，删除延迟消息
end

function SendMayaPalaceCountDownPanel( sysarg )
	local sceneId = Actor.getSceneId( sysarg )
	local palaceCfg = GetMayaPalaceCfgBySceneId( sceneId )
	if not palaceCfg then
		return
	end
	OpenMayaPalaceCountDownPanel( sysarg, palaceCfg )
end

--[[
初始化倒计时面板
]]
function OpenMayaPalaceCountDownPanel( sysarg, palaceCfg )
	local activityId = enCountDownPanelActivity_MayaPalace
	local panelInfo = {}

	local dDvar 		= Actor.getDyanmicVar(sysarg)
	local restTime 		= dDvar.enterMayaPalaceTime + palaceCfg.beInTime - System.getCurrMiniTime()
	if restTime < 0 then
		restTime = 0
	end

	panelInfo.title 	= Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime 	= restTime
	panelInfo.content	= {}
	for bossIdx, bossId in ipairs( palaceCfg.boss ) do
		local bossCfg = MobBossCommonConfig[bossId]
		if bossCfg then
			local desc 	= System.getMonsterNameById( bossCfg.monsterId )
			local lastTime 	= 0
			local bossNum = System.getMonsterCountById( bossCfg.monsterId, bossCfg.sceneId, 0 )
			if bossNum > 0 then		--BOSS存在
				lastTime = 0
			else
				local timeCd = 0
				local monCfg = MobBossCommonConfig[bossCfg.monsterId]
				if monCfg then
					timeCd = GetBossRevivalCd(bossId, monCfg.revivalCd )
				end
				lastTime = GetCommonBossLastTime( bossCfg.monsterId, timeCd ) --GetMayaPalaceBossLastTime( palaceCfg.palaceIdx, bossCfg )
			end
			panelInfo.content[bossIdx] = { desc, lastTime, bossCfg.monsterId }
		end
	end
	OpenCountDownPanel( sysarg, activityId, panelInfo )
end

--[[
倒计时面板内容变化
只发送变化的部分
]]
function ChangeMayaPalaceCountDownPanel( sysarg, palaceCfg, bossId )
	--print("ChangeMayaPalaceCountDownPanel, bossId="..bossId)
	local content = {}
	local bossCfg 	= MobBossCommonConfig[bossId]
	if bossCfg then
		local desc 		= System.getMonsterNameById( bossCfg.monsterId )
		local lastTime 	= 0
		local bossNum 	= System.getMonsterCountById( bossCfg.monsterId, bossCfg.sceneId, 0 )
		if bossNum > 0 then		--BOSS存在
			lastTime = 0
		else
			local timeCd = 0
			local monCfg = MobBossCommonConfig[bossCfg.monsterId]
			if monCfg then
				timeCd = GetBossRevivalCd(bossId, monCfg.revivalCd )
			end
			lastTime = GetCommonBossLastTime( bossCfg.monsterId, timeCd ) --GetMayaPalaceBossLastTime( palaceCfg.palaceIdx, bossCfg )
		end

		local bossIdx = GetBossIdxByBossId( palaceCfg, bossCfg.monsterId )
		if bossIdx > 0 then
			content.idx 	= bossIdx
			content.info	= { desc, lastTime, bossCfg.monsterId }
			--print("ChangeMayaPalaceCountDownPanel, lastTime="..lastTime)
			ChangeCountDownPanel( sysarg, content )
		end
	end
end

function GetBossIdxByBossId( palaceCfg, bossIdIn )
	for bossIdx, bossId in ipairs( palaceCfg.boss ) do
		if bossId == bossIdIn then
			return bossIdx
		end
	end
	return 0
end

function GetMayaPalaceBossLastTime( palaceIdx, oneBoss )
	local dVar = System.getDyanmicVar()
	if not dVar.mayaPalaceBoss then
		dVar.mayaPalaceBoss = {}
	end
	local killTime = dVar.mayaPalaceBoss[palaceIdx][oneBoss.monsterId] or 0
	local lastTime = killTime + oneBoss.revivalCd - System.getCurrMiniTime()
	if lastTime < 0 then
		lastTime = 0
	end
	return lastTime or 0
end

--[[
设置BOSS被击杀的时间
]]
function SetMayaPalaceBossKillTime( palaceIdx, bossId, killTime  )
	local dVar = System.getDyanmicVar()
	if not dVar.mayaPalaceBoss then
		dVar.mayaPalaceBoss = {}
	end

	if not dVar.mayaPalaceBoss[palaceIdx] then
		dVar.mayaPalaceBoss[palaceIdx] = {}
	end
	dVar.mayaPalaceBoss[palaceIdx][bossId] = killTime
end

--boss被刷出
function OnMayaPalaceBossMob(monster, monId, sceneId, x, y)
	--print("OnMayaPalaceBossMob, monId="..monId..", sceneId="..sceneId)
	local palaceCfg = GetMayaPalaceCfgBySceneId( sceneId )
	if not palaceCfg then
		return
	end

	--SetMayaPalaceBossKillTime( palaceCfg.palaceIdx, monId, 0 )			--设置BOSS被击杀的时间
	SetCommonBossKilledTime( monId, 0 )
	BroadMayaPalaceBossChange( palaceCfg, monId )
end

--boss被击杀
--sysarg:归属玩家
--[[
BOSS被击杀（不使用）
lastHitKiller：最后一击玩家
function OnMayaPalaceBossDie(monster, sysarg, monId, lastHitKiller)
	local sceneId = Actor.getSceneId( sysarg )
	--print("OnMayaPalaceBossDie, monId="..monId..", sceneId="..sceneId)
	local palaceCfg = GetMayaPalaceCfgBySceneId( sceneId )
	if not palaceCfg then
		return
	end

	local now = System.getCurrMiniTime()
	--print("OnMayaPalaceBossDie, now="..now)
	--SetMayaPalaceBossKillTime( palaceCfg.palaceIdx, monId, now )			--设置BOSS被击杀的时间
	SetCommonBossKilledTime( monId, now )
	BroadMayaPalaceBossChange( palaceCfg, monId )
end
]]

--[[
boss死亡
]]
function OnMayaPalaceBossDeath(monster, monId)
	local sceneId = Actor.getSceneId( monster )
	--print("OnMayaPalaceBossDeath, monId="..monId..", sceneId="..sceneId)
	local palaceCfg = GetMayaPalaceCfgBySceneId( sceneId )
	if not palaceCfg then
		return
	end

	local now = System.getCurrMiniTime()
	SetCommonBossKilledTime( monId, now )
	BroadMayaPalaceBossChange( palaceCfg, monId )
end

function OnCreateMayaPalaceBoss( npcobj, palaceIdx, bossId )
	--print("OnCreateMayaPalaceBoss, palaceIdx="..palaceIdx..", bossId="..bossId)
	palaceIdx 	= tonumber(palaceIdx)
	bossId 		= tonumber(bossId)
	local palaceCfg = GetMayaPalaceCfg( palaceIdx )
	if palaceCfg then
		for bossIdx, oneBoss in ipairs( palaceCfg.boss ) do
			if oneBoss.monsterId == bossId then
				freshSceneMonsterInPos( oneBoss )		--刷新BOSS，BOSS未击杀则不刷
			end
		end
	end
end

--[[
场景广播BOSS状态变化了
]]
function BroadMayaPalaceBossChange( palaceCfg, bossId )
	local bossNum = System.getMonsterCountById( bossId, palaceCfg.sceneId, 0 )
	--print("BroadMayaPalaceBossChange, bossNum="..bossNum)
	local playerList = LuaHelp.getSceneActorListById( palaceCfg.sceneId ) or {}
	for i,player in ipairs( playerList ) do
		ChangeMayaPalaceCountDownPanel( player, palaceCfg, bossId )
	end
end

--[[获取配置]]
function GetMayaPalaceCfg( palaceIdx )
	--return MayaPalaceConfig[palaceIdx]
	for idx, palaceCfg in ipairs( MayaPalaceConfig ) do
		if palaceCfg.palaceIdx == palaceIdx then
			return palaceCfg
		end
	end
	return nil
end

function GetMayaPalaceCfgBySceneId( sceneId )
	for i, palaceCfg in ipairs( MayaPalaceConfig ) do
		if sceneId == palaceCfg.sceneId then
			return palaceCfg
		end
	end
	return nil
end

--刷出玛雅神殿BOSS
--FreshAllMayaPalaceBoss()				--通过FreshBossWhenStart()刷BOSS

ResisterMayaPalaceProc()				--注册一些事件和方法

